package com.codedancer.campus.mapper;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.codedancer.campus.common.core.BaseMapper;
import com.codedancer.campus.po.Menu;
import java.util.List;

public interface MenuMapper extends BaseMapper<Menu> {

    /**
     * 根据父级uuid获取下级菜单 按照sort升序排序
     *
     * @param parentUuid
     * @return
     */
    default List<Menu> listByParentUuidOrderBySortAsc(String parentUuid) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Menu::getParentUuid, parentUuid);
        lambdaQueryWrapper.orderByAsc(Menu::getSort);
        return selectList(lambdaQueryWrapper);
    }

    /**
     * 获取父级菜单最大的排序
     *
     * @param parentUuid
     * @return
     */
    default Integer selectMaxSortByParentUuid(String parentUuid) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Menu::getParentUuid, parentUuid);
        lambdaQueryWrapper.orderByDesc(Menu::getSort);
        return CollectionUtil.isEmpty(selectList(lambdaQueryWrapper)) ? 0
            : selectList(lambdaQueryWrapper).get(0).getSort();
    }

    /**
     * 根据父级uuid和sort获取菜单
     *
     * @param parentUuid
     * @param sort
     * @return
     */
    default Menu selectMenuByParentUuidAndSort(String parentUuid, int sort) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Menu::getParentUuid, parentUuid);
        lambdaQueryWrapper.eq(Menu::getSort, sort);
        return selectOne(lambdaQueryWrapper);
    }

    /**
     * 判断是否存在菜单儿子
     *
     * @param parentUuid
     * @return
     */
    default boolean isExistsMenuChildren(String parentUuid) {
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Menu::getParentUuid, parentUuid);
        lambdaQueryWrapper.eq(Menu::getIsMenu, true);
        lambdaQueryWrapper.eq(Menu::getIsHidden, false);
        return selectCount(lambdaQueryWrapper) > 0;
    }
}
